package beedoolgi;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

import org.jsoup.Jsoup;
import org.jsoup.helper.DataUtil;
import org.jsoup.nodes.Document;

public class ParallelParser implements Callable<Document> {
	private String name;
	private Document doc;
	private String in;
	private CountDownLatch cdl;
	
	public ParallelParser(String name, File in,  CountDownLatch cdl) throws Exception {
		this.name = name;
		this.in = new String(Files.readAllBytes(Paths.get(in.toString())));;
		this.cdl = cdl;
	}
	
	public ParallelParser(String name, String in,  CountDownLatch cdl) throws Exception {
		this.name = name;
		this.in = in;
		this.cdl = cdl;
	}
	
	public Document call() {
		try {
			long start = System.currentTimeMillis();
			doc = Jsoup.parse(in);
			long end = System.currentTimeMillis();
			//System.out.println(name + " has terminated and passed time is " + (end - start) + "ms");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cdl.countDown();
		}
		return doc;
	}

}
